const util = require('../../../utils/util.js')
const app = getApp()

Page({
  data: {
    CustomBar: app.globalData.CustomBar,
    theme: app.globalData.theme, //全局颜色变量
    tabCur: 0,
    orderStatus: [{
        value: '全部',
        key: ''
      }, {
        value: '施工中',
        key: '2'
      }, {
        value: '已完成',
        key: '3'
      }
      , {
        value: '待评价',
        key: '4'
      }
    ],
    page: {
      searchCount: false,
      current: 1,
      size: 10,
      ascs: '', //升序字段
      descs: 'create_time',
      longitude: null,
      latitude: null
    },
    parameter: {},
    loadmore: true,
    orderList: [],
    completeOrderModal: false,
    orderId: null,
    cancelOrderModal: false
  },
  onShow() {},
  onLoad: function (options) {
    this.setData({
      theme: app.globalData.theme,
      "page.longitude": app.globalData.addressComponent.longitude,
      "page.latitude": app.globalData.addressComponent.latitude
    });
    let that = this
    if (options.date) {
      // 查询这一天的订单数据
      this.setData({
        "page.bookingTime": options.date
      })
    }
    if (options.status) {
      this.setData({
        ['parameter.status']: options.status
      })
      this.data.orderStatus.forEach(function (status, index) {
        if (status.key == options.status) {
          that.setData({
            tabCur: index
          })
        }
      })
    }
    app.initPage()
      .then(res => {
        this.orderPage()
      })
  },
  orderPage() {
    app.api.providerOrderPage(Object.assign({},
        this.data.page,
        util.filterForm(this.data.parameter)
      ))
      .then(res => {
        let orderList = res.data.records
        this.setData({
          orderList: [...this.data.orderList, ...orderList]
        })
        if (orderList.length < this.data.page.size) {
          this.setData({
            loadmore: false
          })
        }
      })
  },
  onReachBottom() {
    if (this.data.loadmore) {
      this.setData({
        ['page.current']: this.data.page.current + 1
      })
      this.orderPage()
    }
  },
  refresh() {
    this.setData({
      loadmore: true,
      orderList: [],
      ['page.current']: 1
    })
    this.orderPage()
  },
  onPullDownRefresh() {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading()
    this.refresh()
    // 隐藏导航栏加载框
    wx.hideNavigationBarLoading()
    // 停止下拉动作
    wx.stopPullDownRefresh()
  },
  tabSelect(e) {
    let dataset = e.currentTarget.dataset
    if (dataset.index != this.data.tabCur) {
      this.setData({
        tabCur: dataset.index,
        ['parameter.status']: dataset.key
      })
      this.refresh()
    }
  },
  orderCancel(e) {
    let index = e.target.dataset.index
    let orderList = this.data.orderList
    app.api.orderGet(orderList[index].id)
      .then(res => {
        this.data.orderList[index] = res.data
        this.setData({
          orderList: this.data.orderList
        })
      })
  },
  orderDel(e) {
    let index = e.target.dataset.index
    this.data.orderList.splice(index, 1)
    this.setData({
      orderList: this.data.orderList
    })
  },
  unifiedOrder(e) {
    let index = e.target.dataset.index
    let orderList = this.data.orderList
    app.api.orderGet(orderList[index].id)
      .then(res => {
        this.data.orderList[index] = res.data
        this.setData({
          orderList: this.data.orderList
        })
      })
  },
  // 跳转地图导航
  jumpMap(e) {
    let index = e.currentTarget.dataset.index
    let order = this.data.orderList[index]
    wx.openLocation({
      latitude: Number(order.latitude),
      longitude: Number(order.longitude),
      scale: 15,
      address: order.userAddress
    })
  },
  // 一键拨号
  bohaoClick(e) {
    let phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  // 取消订单
  cancelOrder(e) {
    let id = e.currentTarget.dataset.id
    this.setData({
      cancelOrderModal: true,
      orderId: id
    })
  },
  hideCancelOrderModal() {
    this.setData({
      cancelOrderModal: false,
      orderId: null
    })
  },
  cancelOrderEvent(e) {
    this.hideCancelOrderModal()
    this.refresh()
  },
  // 接单反馈
  completeOrder(e) {
    let id = e.currentTarget.dataset.id
    this.setData({
      completeOrderModal: true,
      orderId: id
    })
  },
  hideCompleteOrderModal() {
    this.setData({
      completeOrderModal: false,
      orderId: null
    })
  },
  completeOrderEvent(e) {
    this.hideCompleteOrderModal()
    this.refresh()
  }
})